package com.dianrui.agent.activity;


import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.PolygonOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.maps.model.animation.Animation;
import com.amap.api.maps.model.animation.ScaleAnimation;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.dianrui.agent.R;
import com.dianrui.agent.bean.BikePointModel;
import com.dianrui.agent.bean.Colleage;
import com.dianrui.agent.bean.Monitor;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.MyUtil;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 车辆监控
 */
public class CarMonitoringActivity extends BaseActivity implements AMapLocationListener {
    private boolean isFirstLoc = true;
    public AMapLocationClient mLocationClient;
    public AMapLocationClientOption mLocationOption = null;
    private Marker clickMaker;
    public String CUSTOMMAKRER = "Markers";
    private List<Monitor> monitorPointList = new ArrayList<>();
    private List<LatLng> mAreaPoints = new ArrayList<>();
    private List<Colleage> colleageList = new ArrayList<>();
    private AMap aMap;
    private BitmapDescriptor bitD_NoStop;
    private BitmapDescriptor bitD_Start;
    private BitmapDescriptor bitD_Pause;
    private BitmapDescriptor bitD_LowCarEle;
    private BitmapDescriptor bitD_ToubleCarEle;
    private BitmapDescriptor bitD_outsideCarEle;
    private BitmapDescriptor bitD_tomeoutCarEle;
    private BitmapDescriptor bitD_IllegalCarEle;
    private BitmapDescriptor bitD_offlineCarEle;
    private int pressFlag = 0;
    private double latitude;
    private double longitude;
    private List<Marker> mAllCollegeMarker = new ArrayList<>();
    private List<Marker> mAllCarMarker = new ArrayList<>();//marker集合。分类的话通过这个集合里marker全部移除再把整个list清除，这样就算不在地图外的markker给干掉


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.mapviews)
    MapView mapviews;
    @BindView(R.id.scrollView)
    HorizontalScrollView scrollView;
    @BindView(R.id.top_layout)
    LinearLayout topLayout;
    @BindView(R.id.stop_txt)
    TextView stopTxt;
    @BindView(R.id.stop_line)
    View stopLine;
    @BindView(R.id.start_txt)
    TextView startTxt;
    @BindView(R.id.start_line)
    View startLine;
    @BindView(R.id.pause_txt)
    TextView pauseTxt;
    @BindView(R.id.pause_line)
    View pauseLine;
    @BindView(R.id.nopower_txt)
    TextView nopowerTxt;
    @BindView(R.id.nopower_line)
    View nopowerLine;
    @BindView(R.id.trouble_txt)
    TextView troubleTxt;
    @BindView(R.id.trouble_line)
    View troubleLine;
    @BindView(R.id.outside_txt)
    TextView outsideTxt;
    @BindView(R.id.outside_line)
    View outsideLine;
    @BindView(R.id.timeout_txt)
    TextView timeoutTxt;
    @BindView(R.id.timeout_line)
    View timeoutLine;

    @BindView(R.id.Illegalstop_txt)
    TextView IllegalstopTxt;
    @BindView(R.id.Illegalstop_line)
    View IllegalstopLine;

    @BindView(R.id.offline_txt)
    TextView offlineTxt;
    @BindView(R.id.offline_line)
    View offlineLine;

    @BindView(R.id.colleagebtn)
    Button colleagebtn;

    @Override
    public int getContentViewId() {
        return R.layout.car_monitoring;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapviews.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
        title.setText("车辆监控");
        if (aMap == null) {
            aMap = mapviews.getMap();
        }
        getArea();
        getMonitor();
        getStation();
        loadLocation();
        UiSettings mUiSettings = aMap.getUiSettings();

        mUiSettings.setTiltGesturesEnabled(false);
        mUiSettings.setRotateGesturesEnabled(false);
        mUiSettings.setZoomControlsEnabled(false);


        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));// 设置圆形的边框颜色
        myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);

        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。

        aMap.setOnMarkerClickListener(markerClickListener);
        aMap.setInfoWindowAdapter(infoWindowAdapter);
        aMap.setOnInfoWindowClickListener(listener);
        aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                if (clickMaker != null && clickMaker.isInfoWindowShown()) {
                    clickMaker.hideInfoWindow();
                }
            }
        });
        View view1 = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.stop_point, null);
        bitD_NoStop = BitmapDescriptorFactory.fromView(view1);

        View startView = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.over_green_point, null);
        bitD_Start = BitmapDescriptorFactory.fromView(startView);

        View nopauseView = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.pause_point, null);
        bitD_Pause = BitmapDescriptorFactory.fromView(nopauseView);

        View lowPowerView = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.over_red_point, null);
        bitD_LowCarEle = BitmapDescriptorFactory.fromView(lowPowerView);

        View halfToubleView = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.trouble_point, null);

        bitD_ToubleCarEle = BitmapDescriptorFactory.fromView(halfToubleView);

        View outsideView = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.outside_point, null);

        bitD_outsideCarEle = BitmapDescriptorFactory.fromView(outsideView);


        View timeoutView = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.timeout_point, null);

        bitD_tomeoutCarEle = BitmapDescriptorFactory.fromView(timeoutView);


        View IllegalCarView = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.illegalcar_point, null);

        bitD_IllegalCarEle = BitmapDescriptorFactory.fromView(IllegalCarView);


        View offlineCar_View = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.offlinecar_point, null);

        bitD_offlineCarEle = BitmapDescriptorFactory.fromView(offlineCar_View);

    }

    private void loadLocation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(CarMonitoringActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                startLocation();
            } else {
                ActivityCompat.requestPermissions(CarMonitoringActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 2);
            }
        } else {
            startLocation();
        }
    }


    //开始定位
    private void startLocation() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }


    private void getMonitor() {
        StringBuilder carMonritoringBulider = new StringBuilder();
        carMonritoringBulider.append(SharedUtil.getUrl()).append(Url.CAR_MONITORING);
        OkHttpRequest.getInstance().postTokenRequests(carMonritoringBulider.toString(), "", new OkHttpRequest.XCallBack() {

            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            if (monitorPointList.size() > 0) {
                                monitorPointList.clear();
                            }
                            JSONArray jsonArray = jsonObject.optJSONArray("data");
                            if (jsonArray.length() > 0) {
                                int stopcounts = 0;
                                int startcounts = 0;
                                int pausecounts = 0;
                                int nopowercounts = 0;
                                int troublecounts = 0;
                                int outsidecounts = 0;
                                int timeoutcounts = 0;
                                int nostopcounts = 0;
                                int offlinecounts = 0;

                                for (int i = 0; i < jsonArray.length(); i++) {
                                    Monitor monitoringModel = new Monitor();
                                    monitoringModel.lat = jsonArray.optJSONObject(i).optDouble("glat");
                                    monitoringModel.lng = jsonArray.optJSONObject(i).optDouble("glng");
                                    monitoringModel.number = jsonArray.optJSONObject(i).optString("number");
                                    monitoringModel.voltage = jsonArray.optJSONObject(i).optString("voltage");
                                    monitoringModel.city_path = jsonArray.optJSONObject(i).optString("city_path");
                                    monitoringModel.last_time = jsonArray.optJSONObject(i).optLong("last_time");
                                    monitoringModel.status = jsonArray.optJSONObject(i).optString("status");
                                    monitoringModel.status_name = jsonArray.optJSONObject(i).optString("status_name");
                                    monitoringModel.vehicle_status = jsonArray.optJSONObject(i).optInt("vehicle_status");
                                    monitoringModel.battery = jsonArray.optJSONObject(i).optString("battery");
                                    monitoringModel.use_status = jsonArray.optJSONObject(i).optString("use_status");
                                    monitorPointList.add(monitoringModel);
                                    View view = View.inflate(CarMonitoringActivity.this, R.layout.stop_point, null);
                                    Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(BitmapDescriptorFactory.fromView(view)));
                                    mAllCarMarker.add(marker);
                                    marker.setTitle(CUSTOMMAKRER);
                                    marker.setObject(monitorPointList.get(i));

                                    switch (monitorPointList.get(i).vehicle_status) {
                                        case 1:
                                            stopcounts++;
                                            stopTxt.setText(getString(R.string.stop) + "(" + stopcounts + "" + ")");
                                            break;
                                        case 2:
                                            startcounts++;
                                            startTxt.setText(getString(R.string.start) + "(" + startcounts + "" + ")");
                                            break;
                                        case 3:
                                            pausecounts++;
                                            pauseTxt.setText(getString(R.string.pause) + "(" + pausecounts + "" + ")");
                                            break;
                                        case 4:
                                            nopowercounts++;
                                            nopowerTxt.setText(getString(R.string.nopower) + "(" + nopowercounts + "" + ")");
                                            break;
                                        case 5:
                                            troublecounts++;
                                            troubleTxt.setText(getString(R.string.troublecar) + "(" + troublecounts + "" + ")");
                                            break;
                                        case 6:
                                            outsidecounts++;
                                            outsideTxt.setText(getString(R.string.ouside_txt) + "(" + outsidecounts + "" + ")");
                                            break;
                                        case 7:
                                            timeoutcounts++;
                                            timeoutTxt.setText(getString(R.string.timeoutcar) + "(" + timeoutcounts + "" + ")");
                                            break;
                                        case 8:
                                            nostopcounts++;
                                            IllegalstopTxt.setText(getString(R.string.Illegalcar) + "(" + nostopcounts + "" + ")");
                                            break;
                                        case 9:
                                            offlinecounts++;
                                            offlineTxt.setText(getString(R.string.offlinecar) + "(" + offlinecounts + "" + ")");
                                            break;
                                        default:
                                            break;

                                    }


                                }

                            }
                        } else if ("1001".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                        } else if ("1002".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                            spUtils.clear();
                            JumpActivitys(LoginActivity.class);
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    private void getArea() {
        if (!StringUtils.isEmpty(spUtils.getString("area"))) {
            try {
                final JSONObject jsonObject = new JSONObject(spUtils.getString("area"));
                if (jsonObject != null) {
                    if (mAreaPoints != null) {
                        mAreaPoints.clear();
                    }
                    JSONArray gfixed_path = jsonObject.optJSONObject("data").optJSONArray("fixed_path");
                    if (gfixed_path.length() > 0) {
                        for (int j = 0; j < gfixed_path.length(); j++) {
                            JSONObject jsonObject3 = gfixed_path.optJSONObject(j);
                            mAreaPoints.add(new LatLng(jsonObject3.optDouble("lat"), jsonObject3.optDouble("lng")));
                        }

                        if (mAreaPoints.size() > 0) {
                            PolygonOptions polygonOptions = new PolygonOptions();
                            polygonOptions.addAll(mAreaPoints);
                            polygonOptions.strokeWidth(6);
                            polygonOptions.strokeColor(Color.rgb(255, 0, 0));
                            polygonOptions.fillColor(Color.argb(0, 255, 0, 0));
                            aMap.addPolygon(polygonOptions);
                        }
                        LatLngBounds.Builder b = LatLngBounds.builder();

                        for (LatLng latLng : mAreaPoints) {
                            b.include(latLng);
                        }
                        LatLngBounds bounds = b.build();
                        aMap.moveCamera(CameraUpdateFactory.newLatLngBoundsRect(bounds, 80, 80, 230, 260));
                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    //获取还车点
    private void getStation() {
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.NETWORK, "", new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject != null && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        if ("1".equals(jsonArray.optJSONObject(i).optString("fixed_type"))) {
                                            List<LatLng> points = new ArrayList<>();


                                            if (points != null) {
                                                points.clear();
                                            }
                                            JSONArray jsonArray3 = jsonArray.optJSONObject(i).optJSONArray("fixed_path");
                                            if (jsonArray3.length() > 0) {
                                                for (int j = 0; j <= jsonArray3.length(); j++) {
                                                    if (j == jsonArray3.length()) {
                                                        JSONObject jsonObject1 = jsonArray3.optJSONObject(0);
                                                        points.add(new LatLng(jsonObject1.optDouble("lat"), jsonObject1.optDouble("lng")));
                                                    } else {
                                                        JSONObject jsonObject1 = jsonArray3.optJSONObject(j);
                                                        points.add(new LatLng(jsonObject1.optDouble("lat"), jsonObject1.optDouble("lng")));
                                                    }
                                                }
                                                if (points.size() < 2) {
                                                    return;
                                                }
                                                if (points.size() > 0) {
                                                    List<BitmapDescriptor> textureList = new ArrayList<>();
                                                    textureList.add(BitmapDescriptorFactory.fromResource(R.mipmap.customer_img));

                                                    List<Integer> textureIndexs = new ArrayList<>();
                                                    textureIndexs.add(i);
                                                    aMap.addPolyline(new PolylineOptions().
                                                            addAll(points).width(4).setCustomTextureList(textureList).setCustomTextureIndex(textureIndexs).setDottedLine(true));
                                                }
                                            }
                                        }
                                    }
                                }
                            } else if ("1001".equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1002".equals(jsonObject.optString("status"))) {
                                spUtils.clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpActivitys(LoginActivity.class);
                                finish();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    private int tagClick;

    @OnClick({R.id.back, R.id.stop_layout, R.id.start_layout, R.id.colleagebtn, R.id.pause_layout, R.id.nopower_layout, R.id.trouble_layout,
            R.id.bigger, R.id.smaller, R.id.refresh, R.id.location,
            R.id.outside_layout, R.id.timeout_layout, R.id.Illegalstop_layout, R.id.offline_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.stop_layout:
                tagClick = 1;
                stopClick();
                break;
            case R.id.start_layout:
                tagClick = 2;
                startClick();
                break;
            case R.id.pause_layout:
                tagClick = 3;
                pauseClick();
                break;
            case R.id.nopower_layout:
                tagClick = 4;
                noPowerClick();
                break;

            case R.id.trouble_layout:
                tagClick = 5;
                troubleClick();
                break;

            case R.id.outside_layout:
                tagClick = 6;
                outsideClick();
                break;

            case R.id.timeout_layout:
                tagClick = 7;
                timeoutClick();
                break;

            case R.id.Illegalstop_layout:
                tagClick = 8;
                noStopClick();
                break;

            case R.id.offline_layout:
                tagClick = 9;
                offlineClick();
                break;

            case R.id.location://定位
                if (latitude == 0.0000 && longitude == 0.0000) {
                    return;
                }
                aMap.moveCamera(CameraUpdateFactory.zoomTo(16));
                aMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));
                break;

            case R.id.refresh://
                switch (tagClick) {
                    case 1:
                        stopClick();
                        break;
                    case 2:
                        startClick();
                        break;
                    case 3:
                        pauseClick();
                        break;
                    case 4:
                        noPowerClick();
                        break;
                    case 5:
                        troubleClick();
                        break;
                    case 6:
                        outsideClick();
                        break;

                    case 7:
                        timeoutClick();
                        break;

                    case 8:
                        noStopClick();
                        break;

                    case 9:
                        offlineClick();
                        break;

                    default:
                        break;

                }

                break;
            case R.id.bigger:
                MyUtil.setMapStatus(mapviews, MyUtil.getZoom(mapviews) + 1);
                break;
            case R.id.smaller:
                MyUtil.setMapStatus(mapviews, MyUtil.getZoom(mapviews) - 1);
                break;
            case R.id.colleagebtn:
                if (pressFlag == 0) {
                    Colleage();
                    colleagebtn.setText("关闭同事位置");
                    pressFlag = 1;
                } else {
                    colleagebtn.setText("显示同事位置");
                    clearCollegeMarkers();
                    pressFlag = 0;
                }
                break;
            default:
                break;
        }
    }

    /**
     * 清除所有同事Marker
     */
    private void clearCollegeMarkers() {
        if (mAllCollegeMarker.size()>0) {
            for (Marker marker : mAllCollegeMarker) {
                marker.remove();
            }
            mAllCollegeMarker.clear();
        }
    }

    private void clearMarkers() {
        if (mAllCarMarker.size() > 0) {
            for (Marker marker : mAllCarMarker) {
                marker.remove();
            }
            mAllCarMarker.clear();
        }
    }

    private void offlineClick() {
        clearMarkers();
        stopTxt.setTextColor(getResources().getColor(R.color.black));
        stopLine.setVisibility(View.INVISIBLE);
        startTxt.setTextColor(getResources().getColor(R.color.black));
        startLine.setVisibility(View.INVISIBLE);
        pauseTxt.setTextColor(getResources().getColor(R.color.black));
        pauseLine.setVisibility(View.INVISIBLE);
        nopowerTxt.setTextColor(getResources().getColor(R.color.black));
        nopowerLine.setVisibility(View.INVISIBLE);
        troubleTxt.setTextColor(getResources().getColor(R.color.black));
        troubleLine.setVisibility(View.INVISIBLE);
        outsideTxt.setTextColor(getResources().getColor(R.color.black));
        outsideLine.setVisibility(View.INVISIBLE);
        timeoutTxt.setTextColor(getResources().getColor(R.color.black));
        timeoutLine.setVisibility(View.INVISIBLE);
        IllegalstopTxt.setTextColor(getResources().getColor(R.color.black));
        IllegalstopLine.setVisibility(View.INVISIBLE);
        offlineTxt.setTextColor(getResources().getColor(R.color.red1));
        offlineLine.setVisibility(View.VISIBLE);

        for (int i = 0; i < monitorPointList.size(); i++) {
            if (monitorPointList.get(i).vehicle_status == 9) {
                MarkerOptions markerOptions1 = new MarkerOptions();
                markerOptions1.position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(bitD_offlineCarEle);
                Marker marker = aMap.addMarker(markerOptions1);
                mAllCarMarker.add(marker);
                marker.setTitle(CUSTOMMAKRER);
                marker.setObject(monitorPointList.get(i));
                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                marker.setAnimation(markerAnimation);
                marker.startAnimation();

            }
        }
    }

    private void noStopClick() {
        clearMarkers();
        stopTxt.setTextColor(getResources().getColor(R.color.black));
        stopLine.setVisibility(View.INVISIBLE);
        startTxt.setTextColor(getResources().getColor(R.color.black));
        startLine.setVisibility(View.INVISIBLE);
        pauseTxt.setTextColor(getResources().getColor(R.color.black));
        pauseLine.setVisibility(View.INVISIBLE);
        nopowerTxt.setTextColor(getResources().getColor(R.color.black));
        nopowerLine.setVisibility(View.INVISIBLE);
        troubleTxt.setTextColor(getResources().getColor(R.color.black));
        troubleLine.setVisibility(View.INVISIBLE);
        outsideTxt.setTextColor(getResources().getColor(R.color.black));
        outsideLine.setVisibility(View.INVISIBLE);
        timeoutTxt.setTextColor(getResources().getColor(R.color.black));
        timeoutLine.setVisibility(View.INVISIBLE);
        IllegalstopTxt.setTextColor(getResources().getColor(R.color.red1));
        IllegalstopLine.setVisibility(View.VISIBLE);
        offlineTxt.setTextColor(getResources().getColor(R.color.black));
        offlineLine.setVisibility(View.INVISIBLE);
        for (int i = 0; i < monitorPointList.size(); i++) {
            if (monitorPointList.get(i).vehicle_status == 8) {
                MarkerOptions markerOptions1 = new MarkerOptions();
                markerOptions1.position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(bitD_IllegalCarEle);
                Marker marker = aMap.addMarker(markerOptions1);
                mAllCarMarker.add(marker);
                marker.setTitle(CUSTOMMAKRER);
                marker.setObject(monitorPointList.get(i));
                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                marker.setAnimation(markerAnimation);
                marker.startAnimation();

            }

        }
    }

    private void timeoutClick() {
        clearMarkers();
        stopTxt.setTextColor(getResources().getColor(R.color.black));
        stopLine.setVisibility(View.INVISIBLE);
        startTxt.setTextColor(getResources().getColor(R.color.black));
        startLine.setVisibility(View.INVISIBLE);
        pauseTxt.setTextColor(getResources().getColor(R.color.black));
        pauseLine.setVisibility(View.INVISIBLE);
        nopowerTxt.setTextColor(getResources().getColor(R.color.black));
        nopowerLine.setVisibility(View.INVISIBLE);
        troubleTxt.setTextColor(getResources().getColor(R.color.black));
        troubleLine.setVisibility(View.INVISIBLE);
        outsideTxt.setTextColor(getResources().getColor(R.color.black));
        outsideLine.setVisibility(View.INVISIBLE);
        timeoutTxt.setTextColor(getResources().getColor(R.color.red1));
        timeoutLine.setVisibility(View.VISIBLE);
        IllegalstopTxt.setTextColor(getResources().getColor(R.color.black));
        IllegalstopLine.setVisibility(View.INVISIBLE);
        offlineTxt.setTextColor(getResources().getColor(R.color.black));
        offlineLine.setVisibility(View.INVISIBLE);

        for (int i = 0; i < monitorPointList.size(); i++) {
            if (monitorPointList.get(i).vehicle_status == 7) {
                MarkerOptions markerOptions1 = new MarkerOptions();
                markerOptions1.position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(bitD_tomeoutCarEle);
                Marker marker = aMap.addMarker(markerOptions1);
                mAllCarMarker.add(marker);
                marker.setTitle(CUSTOMMAKRER);
                marker.setObject(monitorPointList.get(i));
                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                marker.setAnimation(markerAnimation);
                marker.startAnimation();

            }

        }
    }

    private void outsideClick() {
        clearMarkers();
        stopTxt.setTextColor(getResources().getColor(R.color.black));
        stopLine.setVisibility(View.INVISIBLE);
        startTxt.setTextColor(getResources().getColor(R.color.black));
        startLine.setVisibility(View.INVISIBLE);
        pauseTxt.setTextColor(getResources().getColor(R.color.black));
        pauseLine.setVisibility(View.INVISIBLE);
        nopowerTxt.setTextColor(getResources().getColor(R.color.black));
        nopowerLine.setVisibility(View.INVISIBLE);
        troubleTxt.setTextColor(getResources().getColor(R.color.black));
        troubleLine.setVisibility(View.INVISIBLE);
        outsideTxt.setTextColor(getResources().getColor(R.color.red1));
        outsideLine.setVisibility(View.VISIBLE);
        timeoutTxt.setTextColor(getResources().getColor(R.color.black));
        timeoutLine.setVisibility(View.INVISIBLE);
        IllegalstopTxt.setTextColor(getResources().getColor(R.color.black));
        IllegalstopLine.setVisibility(View.INVISIBLE);
        offlineTxt.setTextColor(getResources().getColor(R.color.black));
        offlineLine.setVisibility(View.INVISIBLE);


        for (int i = 0; i < monitorPointList.size(); i++) {
            if (monitorPointList.get(i).vehicle_status == 6) {
                MarkerOptions markerOptions1 = new MarkerOptions();
                markerOptions1.position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(bitD_outsideCarEle);
                Marker marker = aMap.addMarker(markerOptions1);
                mAllCarMarker.add(marker);
                marker.setTitle(CUSTOMMAKRER);
                marker.setObject(monitorPointList.get(i));
                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                marker.setAnimation(markerAnimation);
                marker.startAnimation();
            }

        }
    }

    private void troubleClick() {
        clearMarkers();
        stopTxt.setTextColor(getResources().getColor(R.color.black));
        stopLine.setVisibility(View.INVISIBLE);
        startTxt.setTextColor(getResources().getColor(R.color.black));
        startLine.setVisibility(View.INVISIBLE);
        pauseTxt.setTextColor(getResources().getColor(R.color.black));
        pauseLine.setVisibility(View.INVISIBLE);
        nopowerTxt.setTextColor(getResources().getColor(R.color.black));
        nopowerLine.setVisibility(View.INVISIBLE);
        troubleTxt.setTextColor(getResources().getColor(R.color.red1));
        troubleLine.setVisibility(View.VISIBLE);
        outsideTxt.setTextColor(getResources().getColor(R.color.black));
        outsideLine.setVisibility(View.INVISIBLE);
        timeoutTxt.setTextColor(getResources().getColor(R.color.black));
        timeoutLine.setVisibility(View.INVISIBLE);
        IllegalstopTxt.setTextColor(getResources().getColor(R.color.black));
        IllegalstopLine.setVisibility(View.INVISIBLE);
        offlineTxt.setTextColor(getResources().getColor(R.color.black));
        offlineLine.setVisibility(View.INVISIBLE);

        for (int i = 0; i < monitorPointList.size(); i++) {
            if (monitorPointList.get(i).vehicle_status == 5) {
                MarkerOptions markerOptions1 = new MarkerOptions();
                markerOptions1.position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(bitD_ToubleCarEle);
                Marker marker = aMap.addMarker(markerOptions1);
                mAllCarMarker.add(marker);
                marker.setTitle(CUSTOMMAKRER);
                marker.setObject(monitorPointList.get(i));
                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                marker.setAnimation(markerAnimation);
                marker.startAnimation();
            }

        }
    }

    private void noPowerClick() {
        clearMarkers();
        stopTxt.setTextColor(getResources().getColor(R.color.black));
        stopLine.setVisibility(View.INVISIBLE);
        startTxt.setTextColor(getResources().getColor(R.color.black));
        startLine.setVisibility(View.INVISIBLE);
        pauseTxt.setTextColor(getResources().getColor(R.color.black));
        pauseLine.setVisibility(View.INVISIBLE);
        nopowerTxt.setTextColor(getResources().getColor(R.color.red1));
        nopowerLine.setVisibility(View.VISIBLE);
        troubleTxt.setTextColor(getResources().getColor(R.color.black));
        troubleLine.setVisibility(View.INVISIBLE);
        outsideTxt.setTextColor(getResources().getColor(R.color.black));
        outsideLine.setVisibility(View.INVISIBLE);
        timeoutTxt.setTextColor(getResources().getColor(R.color.black));
        timeoutLine.setVisibility(View.INVISIBLE);
        IllegalstopTxt.setTextColor(getResources().getColor(R.color.black));
        IllegalstopLine.setVisibility(View.INVISIBLE);
        offlineTxt.setTextColor(getResources().getColor(R.color.black));
        offlineLine.setVisibility(View.INVISIBLE);

        for (int i = 0; i < monitorPointList.size(); i++) {
            if (monitorPointList.get(i).vehicle_status == 4) {
                MarkerOptions markerOptions1 = new MarkerOptions();
                markerOptions1.position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(bitD_LowCarEle);
                Marker marker = aMap.addMarker(markerOptions1);
                mAllCarMarker.add(marker);
                marker.setObject(monitorPointList.get(i));
                marker.setTitle(CUSTOMMAKRER);
                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                marker.setAnimation(markerAnimation);
                marker.startAnimation();
            }

        }
    }

    private void pauseClick() {
        clearMarkers();
        stopTxt.setTextColor(getResources().getColor(R.color.black));
        stopLine.setVisibility(View.INVISIBLE);
        startTxt.setTextColor(getResources().getColor(R.color.black));
        startLine.setVisibility(View.INVISIBLE);
        pauseTxt.setTextColor(getResources().getColor(R.color.red1));
        pauseLine.setVisibility(View.VISIBLE);
        nopowerTxt.setTextColor(getResources().getColor(R.color.black));
        nopowerLine.setVisibility(View.INVISIBLE);
        troubleTxt.setTextColor(getResources().getColor(R.color.black));
        troubleLine.setVisibility(View.INVISIBLE);
        outsideTxt.setTextColor(getResources().getColor(R.color.black));
        outsideLine.setVisibility(View.INVISIBLE);
        timeoutTxt.setTextColor(getResources().getColor(R.color.black));
        timeoutLine.setVisibility(View.INVISIBLE);
        IllegalstopTxt.setTextColor(getResources().getColor(R.color.black));
        IllegalstopLine.setVisibility(View.INVISIBLE);
        offlineTxt.setTextColor(getResources().getColor(R.color.black));
        offlineLine.setVisibility(View.INVISIBLE);


        for (int i = 0; i < monitorPointList.size(); i++) {
            if (monitorPointList.get(i).vehicle_status == 3) {
                MarkerOptions markerOptions1 = new MarkerOptions();
                markerOptions1.position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(bitD_Pause);
                Marker marker = aMap.addMarker(markerOptions1);
                mAllCarMarker.add(marker);
                marker.setTitle(CUSTOMMAKRER);
                marker.setObject(monitorPointList.get(i));
                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                marker.setAnimation(markerAnimation);
                marker.startAnimation();
            }
        }
    }

    private void startClick() {
        clearMarkers();
        stopTxt.setTextColor(getResources().getColor(R.color.black));
        stopLine.setVisibility(View.INVISIBLE);
        startTxt.setTextColor(getResources().getColor(R.color.red1));
        startLine.setVisibility(View.VISIBLE);
        pauseTxt.setTextColor(getResources().getColor(R.color.black));
        pauseLine.setVisibility(View.INVISIBLE);
        nopowerTxt.setTextColor(getResources().getColor(R.color.black));
        nopowerLine.setVisibility(View.INVISIBLE);
        troubleTxt.setTextColor(getResources().getColor(R.color.black));
        troubleLine.setVisibility(View.INVISIBLE);
        outsideTxt.setTextColor(getResources().getColor(R.color.black));
        outsideLine.setVisibility(View.INVISIBLE);
        timeoutTxt.setTextColor(getResources().getColor(R.color.black));
        timeoutLine.setVisibility(View.INVISIBLE);
        IllegalstopTxt.setTextColor(getResources().getColor(R.color.black));
        IllegalstopLine.setVisibility(View.INVISIBLE);
        offlineTxt.setTextColor(getResources().getColor(R.color.black));
        offlineLine.setVisibility(View.INVISIBLE);


        for (int i = 0; i < monitorPointList.size(); i++) {
            if (monitorPointList.get(i).vehicle_status == 2) {
                MarkerOptions markerOptions1 = new MarkerOptions();
                markerOptions1.position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(bitD_Start);
                Marker marker = aMap.addMarker(markerOptions1);
                mAllCarMarker.add(marker);
                marker.setTitle(CUSTOMMAKRER);
                marker.setObject(monitorPointList.get(i));
                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                marker.setAnimation(markerAnimation);
                marker.startAnimation();

            }
        }
    }

    private void stopClick() {
        clearMarkers();
        stopTxt.setTextColor(getResources().getColor(R.color.red1));
        stopLine.setVisibility(View.VISIBLE);
        startTxt.setTextColor(getResources().getColor(R.color.black));
        startLine.setVisibility(View.INVISIBLE);
        pauseTxt.setTextColor(getResources().getColor(R.color.black));
        pauseLine.setVisibility(View.INVISIBLE);
        nopowerTxt.setTextColor(getResources().getColor(R.color.black));
        nopowerLine.setVisibility(View.INVISIBLE);
        troubleTxt.setTextColor(getResources().getColor(R.color.black));
        troubleLine.setVisibility(View.INVISIBLE);
        outsideTxt.setTextColor(getResources().getColor(R.color.black));
        outsideLine.setVisibility(View.INVISIBLE);
        timeoutTxt.setTextColor(getResources().getColor(R.color.black));
        timeoutLine.setVisibility(View.INVISIBLE);
        IllegalstopTxt.setTextColor(getResources().getColor(R.color.black));
        IllegalstopLine.setVisibility(View.INVISIBLE);
        offlineTxt.setTextColor(getResources().getColor(R.color.black));
        offlineLine.setVisibility(View.INVISIBLE);

        for (int i = 0; i < monitorPointList.size(); i++) {
            if (monitorPointList.get(i).vehicle_status == 1) {
                MarkerOptions markerOptions1 = new MarkerOptions();
                markerOptions1.position(new LatLng(monitorPointList.get(i).lat, monitorPointList.get(i).lng)).icon(bitD_NoStop);
                Marker marker = aMap.addMarker(markerOptions1);
                mAllCarMarker.add(marker);
                marker.setTitle(CUSTOMMAKRER);
                marker.setObject(monitorPointList.get(i));
                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                marker.setAnimation(markerAnimation);
                marker.startAnimation();
            }

        }
    }

    // 定义 Marker 点击事件监听
    AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(final Marker marker) {
            clickMaker = marker;
            marker.showInfoWindow();
            return true;
        }
    };

    AMap.InfoWindowAdapter infoWindowAdapter = new AMap.InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker marker) {
            if (marker.getTitle().equals("Colleage")) {
                View colleageView = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.colleage_bubble, null);
                TextView name = colleageView.findViewById(R.id.name);
                TextView workstate = colleageView.findViewById(R.id.workstate);
                TextView mobile = colleageView.findViewById(R.id.mobile);
                final Colleage colleage = (Colleage) marker.getObject();

                StringBuilder nameBuilder = new StringBuilder();
                nameBuilder.append(getString(R.string.name)).append(colleage.nickname);
                name.setText(nameBuilder.toString());

                StringBuilder workstateuilder = new StringBuilder();
                workstateuilder.append(getString(R.string.workstate)).append(colleage.working_state);
                workstate.setText(workstateuilder.toString());


                StringBuilder mobileuilder = new StringBuilder();
                mobileuilder.append(getString(R.string.mobilenumber)).append(colleage.mobile);
                mobile.setText(mobileuilder.toString());

                mobile.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        callPhone(colleage.mobile);
                    }
                });

                return colleageView;
            } else {
                View view = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.show_location_point_bubble, null);
                final TextView number = view.findViewById(R.id.number);
                TextView carbattery = view.findViewById(R.id.carbattery);
                TextView status = view.findViewById(R.id.status);//车辆状态
                TextView time = view.findViewById(R.id.time);
                TextView address = view.findViewById(R.id.address);

                final Monitor info = (Monitor) marker.getObject();

                StringBuilder carNumberString = new StringBuilder();
                carNumberString.append("车辆编号:").append(info.number);
                number.setText(carNumberString.toString());

                StringBuilder carbatteryString = new StringBuilder();
                carbatteryString.append("电量:").append(info.battery).append("%");
                carbattery.setText(carbatteryString.toString());


                StringBuilder carStatusString = new StringBuilder();
                carStatusString.append("设备状态:").append(info.status_name);
                status.setText(carStatusString.toString());

                StringBuilder carlastTimeString = new StringBuilder();
                String dates = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(info.last_time * 1000));
                carlastTimeString.append("最后启动时间:").append(dates);
                time.setText(carlastTimeString.toString());

                StringBuilder addressString = new StringBuilder();
                if (!StringUtils.isEmpty(info.city_path)) {
                    addressString.append("车辆地址:").append(info.city_path);
                } else {
                    addressString.append("车辆地址:无");
                }
                address.setText(addressString.toString());
                return view;
            }

        }

        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }
    };


    //点击marker就消失
    AMap.OnInfoWindowClickListener listener = new AMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            marker.hideInfoWindow();
        }
    };

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapviews.onSaveInstanceState(outState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapviews.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapviews.onPause();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapviews.onDestroy();
        if (monitorPointList != null) {
            monitorPointList.clear();
        }
        if (mAreaPoints != null) {
            mAreaPoints.clear();
        }
        if (mAllCollegeMarker != null) {
            mAllCollegeMarker.clear();
            mAllCollegeMarker = null;
        }
        if (mAllCarMarker != null) {
            mAllCarMarker.clear();
            mAllCarMarker = null;
        }
        bitD_NoStop.recycle();
        bitD_Start.recycle();
        bitD_Pause.recycle();
        bitD_LowCarEle.recycle();
        bitD_ToubleCarEle.recycle();
        bitD_outsideCarEle.recycle();
        bitD_tomeoutCarEle.recycle();
        bitD_offlineCarEle.recycle();
    }

    //获取同事位置
    public void Colleage() {
        StringBuilder colleageBulider = new StringBuilder();
        colleageBulider.append(SharedUtil.getUrl()).append(Url.COLLEAGES);
        OkHttpRequest.getInstance().postTokenRequests(colleageBulider.toString(), "", new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    if (isFinishing()) {
                        return;
                    }
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            if (colleageList != null) {
                                colleageList.clear();
                            }
                            JSONArray jsonArray = jsonObject.optJSONArray("data");
                            if (jsonArray.length() > 0) {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    Colleage colleage = new Colleage();
                                    colleage.glat = jsonArray.optJSONObject(i).optDouble("glat");
                                    colleage.glng = jsonArray.optJSONObject(i).optDouble("glng");
                                    colleage.nickname = jsonArray.optJSONObject(i).optString("nickname");
                                    colleage.face = jsonArray.optJSONObject(i).optString("face");
                                    colleage.mobile = jsonArray.optJSONObject(i).optString("mobile");
                                    colleage.working_state = jsonArray.optJSONObject(i).optString("working_state");
                                    colleageList.add(colleage);
                                }
                                for (int i = 0; i < colleageList.size(); i++) {
                                    final MarkerOptions markerOption = new MarkerOptions();
                                    markerOption.position(new LatLng(colleageList.get(i).glat, colleageList.get(i).glng)).zIndex(100);
                                    final View view = LayoutInflater.from(CarMonitoringActivity.this).inflate(R.layout.colleagepoint, null);
                                    final ImageView colleage_img = view.findViewById(R.id.colleage_img);
                                    try {
                                        final int finalI = i;
                                        SimpleTarget locationTarget = new SimpleTarget<Bitmap>() {
                                            @Override
                                            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                                colleage_img.setImageBitmap(resource);
                                                Bitmap bitmap = getViewBitmap(view);
                                                markerOption.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
                                                Marker marker = aMap.addMarker(markerOption);
                                                mAllCollegeMarker.add(marker);
                                                Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画
                                                markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒
                                                marker.setAnimation(markerAnimation);
                                                marker.startAnimation();
                                                marker.setObject(colleageList.get(finalI));
                                                marker.setTitle("Colleage");
                                                marker.setMarkerOptions(markerOption);
                                            }
                                        };

                                        RequestBuilder lRequestBuilder = Glide.with(getApplication())
                                                .asBitmap()
                                                .load(colleageList.get(i).face).apply(new RequestOptions()
                                                        .transforms(new RoundedCorners(800)));

                                        lRequestBuilder.into(locationTarget);

                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

                                }
                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {}
        });
    }

    public Bitmap getViewBitmap(View view) {
        view.setDrawingCacheEnabled(true);
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        return Bitmap.createBitmap(view.getDrawingCache());
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
            latitude = aMapLocation.getLatitude();
            longitude = aMapLocation.getLongitude();
            if (isFirstLoc) {
                isFirstLoc = false;

            }
        }
    }

    public void callPhone(String phoneNum) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(CarMonitoringActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNum));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } else {
                ActivityCompat.requestPermissions(CarMonitoringActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
            }
        } else {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNum));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

}
